package com.bawei.park2302.park


import android.widget.SearchView
import androidx.activity.viewModels
import androidx.core.view.GravityCompat
import androidx.lifecycle.lifecycleScope
import androidx.paging.PagingData
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.lib_base.utils.utils.entity.ParkBeanItem
import com.bawei.lib_base.utils.utils.mvi.BaseActivity
import com.bawei.lib_base.utils.utils.mvi.UiState
import com.bawei.park2302.R
import com.bawei.park2302.adapter.OnItemClickListener
import com.bawei.park2302.adapter.OnItemLongClickListener
import com.bawei.park2302.adapter.ParkAdapter
import com.bawei.park2302.databinding.ActivityParkBinding
import com.bawei.park2302.notice.NoticeIntent
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch


@Route(path = "/app/ParkActivity")
@AndroidEntryPoint
class ParkActivity : BaseActivity<ActivityParkBinding,ParkIntent,ParkViewModel>() {
    override val binding: ActivityParkBinding by lazy {
        ActivityParkBinding.inflate(layoutInflater)
    }
    override val vm: ParkViewModel by viewModels<ParkViewModel>()

    val parkAdapter by lazy { ParkAdapter() }
    var type = "区域"
    var para = ""

    override fun initView() {
        binding.rvPark.apply {
            adapter = parkAdapter
            layoutManager = LinearLayoutManager(this@ParkActivity)
        }

        //长按进入VR预览
        parkAdapter.setOnItemChildLongClick(object : OnItemLongClickListener{
            override fun onItemClick(bean: ParkBeanItem) {
                ARouter.getInstance().build("/app/ParkVrActivity").navigation()
            }
        })

        //点击修改车位状态
        parkAdapter.setOnItemChildClick(object : OnItemClickListener{
            override fun onItemClick(position: Int,bean: ParkBeanItem) {
                ARouter.getInstance().build("/app/ParkSpacesActivity")
                    .withString("pid",bean.id.toString()).navigation()
            }
        })

        //点击添加车位
        binding.ivAdd.setOnClickListener {
            ARouter.getInstance().build("/app/ParkAddActivity").navigation()
        }

        //显示副窗口
        binding.parkFilter.setOnClickListener {
            binding.drawer.openDrawer(GravityCompat.END)
        }

        //点击搜索
        binding.parkSearchView.setOnQueryTextListener(object : androidx.appcompat.widget.SearchView.OnQueryTextListener {
            override fun onQueryTextSubmit(query: String?): Boolean {
                para=query!!
                if(type=="区域") {
                    vm.sendIntent(ParkIntent.parkArea(query!!))
                }else{
                    vm.sendIntent(ParkIntent.parkLicensePlate(query!!))
                }
                return true
            }

            override fun onQueryTextChange(newText: String?): Boolean {
                if(newText.isNullOrEmpty()){
                    vm.sendIntent(ParkIntent.parkSpaceType("标准车位"))
                }
                return true
            }
        })

        //根据车位类型搜索
        binding.parkRgtype.setOnCheckedChangeListener { radioGroup, checkId ->
            var type=""
            when(checkId){
                R.id.rb_typea->{
                    type="标准车位"
                    binding.rbTypea.isChecked=false
                }
                R.id.rb_typeb->{
                    type="VIP车位"
                    binding.rbTypeb.isChecked=false
                }
            }
            vm.sendIntent(ParkIntent.parkSpaceType(type))
            binding.drawer.closeDrawer(GravityCompat.END)
        }

        //根据车位状态搜索
        binding.drawRg.setOnCheckedChangeListener { radioGroup, checkId ->
            var status = ""
            when(checkId){
                R.id.rb_kx ->{
                    status = "空闲"
                }
                R.id.rb_zy ->{
                    status = "已占用"
                }
                R.id.rb_wh ->{
                    status = "维护中"
                }
            }
            vm.sendIntent(ParkIntent.parkSpaceStatus(status))
            binding.drawer.closeDrawer(GravityCompat.END)
        }

        //根据车牌号搜索
        binding.drawCarNo.setOnClickListener {
            type="车牌号"
            vm.sendIntent(ParkIntent.parkLicensePlate(para))
            binding.drawer.closeDrawer(GravityCompat.END)
        }
    }

    override fun initData() {
        lifecycleScope.launch {
            vm.uiState.collect{
                when(it){
                    is UiState.onSuccess<*> -> {
                        val data = it.data as PagingData<ParkBeanItem>
                        parkAdapter.submitData(lifecycle,data)
                    }

                    else -> {}
                }
            }
        }
    }

    override fun onResume() {
        super.onResume()
        vm.sendIntent(ParkIntent.parkSpaceType("标准车位"))
    }
}